﻿/****************************************************
	文件：MySqlMgr.cs
	作者：HuskyT
	邮箱:  1005240602@qq.com
	日期：2020/05/02 0:33   	
	功能：MySql 管理类
*****************************************************/

using MySql.Data;
using SqlSugar;
using System;
using System.Linq;

namespace MySql
{
    public class MySqlMgr : Singleton<MySqlMgr>
    {
#if DEBUG
        private const string CONNECTION_STR = "server=localhost;uid=root;pwd=;database=studydb";
#else
        private const string CONNECTION_STR = "server=;uid=;pwd=;database=";
#endif
        /// <summary>
        /// 数据库
        /// </summary>
        public SqlSugarClient Db;

        public void Init()
        {
            Db = new SqlSugarClient(new ConnectionConfig
            {
                ConnectionString = CONNECTION_STR,
                DbType = DbType.MySql,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute,
            });

            //用来打印Sql方便你调式    
            Db.Aop.OnLogExecuting = (sql, pars) =>
            {
                Console.WriteLine(sql + "\r\n" +
                Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                Console.WriteLine();
            };
            try
            {
                Db.Queryable<User>().InSingle(1);
                Debug.LogInfo("数据库连接成功");
            }
            catch (Exception e)
            {
                Debug.LogError("数据库连接失败，Exception：" + e.ToString());
            }
        }
    }
}